package commrun

import (
	"context"
	"core/auth"
	"core/coreentity"
	"core/coreservice"
	"core/corestruct"
	"core/coreutil"
	"core/datetime"
	"core/db"
)

type dbSetup struct {
	ctx   *corestruct.HttpContext
	lpage *[]coreentity.HtPage
	lact  *[]coreentity.HtAction
}

func (p *dbSetup) Setup(ctx1 *corestruct.HttpContext) error {
	p.ctx = ctx1

	err := p.HtUser()
	if err != nil {
		return err
	}

	err = p.HtDept()
	if err != nil {
		return err
	}

	err = p.HtRole()
	if err != nil {
		return err
	}

	err = p.HtPlat()
	if err != nil {
		return err
	}

	err = p.HtPage()
	if err != nil {
		return err
	}

	return nil
}

func (p *dbSetup) HtUser() error {
	var muser = &coreentity.HtUser{
		Id:       2000,
		Name:     "通讯管理员",
		Ename:    "commadmin",
		Pwd:      auth.CreatePwd("commadmin", "111111"),
		Czpwd:    "",
		Mobile:   "15655554567",
		Phone:    "",
		Sex:      "",
		Url:      "",
		Zw:       "",
		Birthday: datetime.Now(),
		Address:  "",
		Email:    "",
		IdCode:   "",
		Memo:     "",
		HtareaId: 0,
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx := db.DbMaster.Find(muser)
	if tx.RowsAffected > 0 {
		return nil
	}
	tx = db.DbMaster.Create(muser)
	if tx.Error != nil {
		return tx.Error
	}

	var mur = &coreentity.HtUserRole{
		Id:       2000,
		HtuserId: 2000,
		HtdeptId: 2000,
		HtroleId: 2000,
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx = db.DbMaster.Create(mur)
	if tx.Error != nil {
		return tx.Error
	}

	var mud = &coreentity.HtUserDept{
		Id:       2000,
		HtuserId: 2000,
		HtdeptId: 2000,
		State:    "启用",
		CrtDt:    datetime.Now(),
		CrtUid:   1000,
		CrtDid:   1000,
		EdtDt:    datetime.Now(),
		EdtUid:   1000,
		Gid:      coreutil.IdUtil.UuidStr(),
		Deleted:  0,
	}
	tx = db.DbMaster.Create(mud)
	if tx.Error != nil {
		return tx.Error
	}
	return nil
}

func (p *dbSetup) HtDept() error {
	var m = &coreentity.HtDept{
		Id:         2000,
		Name:       "通讯管理",
		Pid:        0,
		RootId:     0,
		HtplatId:   2000,
		HtareaId:   1,
		Address:    "",
		Dutyperson: "",
		Tel:        "",
		Memo:       "",
		Url:        "",
		State:      "",
		Lng:        0,
		Lat:        0,
		CrtDt:      datetime.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      datetime.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}
	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) HtRole() error {
	var m = &coreentity.HtRole{
		Id:       2000,
		Name:     "通讯管理",
		HtplatId: 2000,
		Memo:     "",

		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}

	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) HtPlat() error {
	var m = &coreentity.HtPlat{
		Id:   2000,
		Pid:  0,
		Name: "通讯管理",
		Memo: "",
	}
	tx := db.DbMaster.Find(m)
	if tx.RowsAffected > 0 {
		return nil
	}

	tx = db.DbMaster.Create(m)
	return tx.Error
}

func (p *dbSetup) findPage(code string) *coreentity.HtPage {
	for index, _ := range *p.lpage {
		if (*p.lpage)[index].Code == code {
			return &((*p.lpage)[index])
		}
	}
	return nil
}
func (p *dbSetup) findAct(url string) *coreentity.HtAction {
	for index, _ := range *p.lact {
		if (*p.lact)[index].FullUrl == url {
			return &((*p.lact)[index])
		}
	}
	return nil
}

func (p *dbSetup) HtPage() error {
	var tx = db.DbMaster.Model(&coreentity.HtPage{})
	var lstsvr = &coreservice.ListService[coreentity.HtPage]{}
	r, err := lstsvr.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lpage = r

	var lstsvract = &coreservice.ListService[coreentity.HtAction]{}
	ract, err := lstsvract.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lact = ract

	err = p.HtPagecommportindex()
	if err != nil {
		return err
	}

	return nil
}
func (p *dbSetup) HtPagecommportindex() error {
	var code = "/commport/index"
	var mdb = p.findPage(code)
	if mdb != nil {
		return nil
	}
	var name = "端口管理"
	mdb = &coreentity.HtPage{
		Id:      coreutil.IdUtil.GetId(),
		Name:    name,
		Code:    code,
		Memo:    "",
		Imgurl:  "layui-icon-form",
		Lx:      "通讯",
		P1:      "",
		P2:      "",
		P3:      "",
		P4:      "",
		CrtDt:   datetime.Now(),
		CrtUid:  1000,
		CrtDid:  1000,
		EdtDt:   datetime.Now(),
		EdtUid:  1000,
		Gid:     coreutil.IdUtil.UuidStr(),
		Deleted: 0,
	}
	var db = p.ctx.DB
	tx2 := coreservice.BaseService.Insert(mdb, db)
	return tx2.Error
}

func DBSetup() {
	var ctx = &corestruct.HttpContext{
		Id: 0,
		Session: &corestruct.HttpSession{
			CrtUid:   1000,
			HtdeptId: 1000,
		},
		DB: db.DbMaster,
	}

	dbctx := context.WithValue(context.Background(), "MyContext", ctx)
	var db = db.DbMaster.WithContext(dbctx)
	ctx.DB = db

	var p = &dbSetup{
		ctx: ctx,
	}
	p.Setup(ctx)
}
